package plgnet

import (
	"dgo/MyProj/PrjDBUtils"
	"dgo/MyProj/PrjHttpUtils"
	"net/http"
)

var (
	DEBUG_FLAG int = 0
	mux            = http.NewServeMux()
)

type IHttpStatus interface {
	OnHttpStatus(rec *PlgHttpRec) (rval interface{}, errcode int, err error)
}

type IHttpIndex interface {
	OnHttpIndex(rec *PlgHttpRec) (rval interface{}, errcode int, err error)
}

type IHttpDo interface {
	OnHttpDo(rec *PlgHttpRec) (rval interface{}, errcode int, err error)
}

type IHttpReload interface {
	OnHttpReload(rec *PlgHttpRec) (rval interface{}, errcode int, err error)
}

type PlgHttpRec struct {
	PrjHttpUtils.HttpRec
	PlugPath       string // /plug/dcloud/project   -> /project
	UriAction      string
	BeforeResponse func() bool

	DAO *PrjDBUtils.DAOHelper
}

func NewPlgHttpRec(w http.ResponseWriter, r *http.Request) *PlgHttpRec {
	rval := &PlgHttpRec{}
	rval.ReqR = r
	rval.RespW = w
	rval.Initial()
	rval.EncodeJSONEscape = true

	if DEBUG_FLAG == 1 {
		rval.EncodeJSONEscape = false
	}

	if PrjHttpUtils.Config_DefaultAllowCrossAccess {
		PrjHttpUtils.AllowCrossAccessSet(w)
	}

	return rval
}

func (this *PlgHttpRec) Close() {
	if this.DAO != nil {
		this.DAO.Close()
		this.DAO = nil
	}

	//if this.ReqR != nil && this.ReqR.Body != nil {
	//	this.ReqR.Body.Close()
	//}

	if this.Req != nil {
		//this.Req.Clear()  // 不清理，怕引用
		this.Req = nil
	}
	if this.Resp != nil {
		//this.Resp.Clear() // 不清理，怕引用
		this.Resp = nil
	}
}
